<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class ChannelList extends \app\BaseModel
{
    protected $name = "massage_channel_list";
    protected $append = ["cate_text"];
    public function getTrueUserNameAttr($value, $data)
    {
        if(isset($value)) {
            if(!empty($value)) {
                return $value;
            }
            if(!empty($data["user_name"])) {
                return $data["user_name"];
            }
        }
    }
    public function getMobileAttr($value, $data)
    {
        if(!empty($value) && isset($data["uniacid"]) && numberEncryption($data["uniacid"]) == 1) {
            return substr_replace($value, "****", 2, 4);
        }
        return $value;
    }
    public function getCateTextAttr($value, $data)
    {
        if(isset($data["cate_id"])) {
            $cate_model = new ChannelCate();
            return $cate_model->where(["id" => $data["cate_id"]])->value("title");
        }
    }
    public function getAdminIdAttr($value, $data)
    {
        if(!empty($value)) {
            $admin_model = new Admin();
            $admin = $admin_model->where(["id" => $value, "status" => 1, "channel_auth" => 1])->count();
            return 0 < $admin ? $value : 0;
        }
        return 0;
    }
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page = 10, $field = "*")
    {
        $data = $this->where($dis)->field($field)->order("id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function adminDataList($dis, $page = 10, $where = [])
    {
        $data = $this->alias("a")->join("massage_service_user_list b", "a.user_id = b.id", "left")->where($dis)->where(function ($query) use ($where) {
            $query->whereOr($where);
        })->field("a.*,b.nickName,b.avatarUrl")->group("a.id")->order("a.id desc")->paginate($page)->toArray();
        return $data;
    }
    public function channelBalance($order)
    {
        if(getPromotionRoleAuth(2, $order["uniacid"]) == 0) {
            $order["channel_id"] = 0;
            $order["salesman_id"] = 0;
        }
        $config = getConfigSettingArr($order["uniacid"], ["salesman_channel_fx_type", "channel_balance", "salesman_balance", "channel_coach_balance", "channel_admin_balance", "salesman_coach_balance", "salesman_admin_balance"]);
        $channel_model = new ChannelList();
        $channel_balance = $channel_model->where(["id" => $order["channel_id"]])->value("balance");
        if(0 <= $channel_balance) {
            $config["channel_balance"] = $channel_balance;
        }
        if(!empty($order["channel_qr_id"]) && !empty($order["channel_id"])) {
            $qr_model = new ChannelQr();
            $qr = $qr_model->dataInfo(["id" => $order["channel_qr_id"], "status" => 1]);
            if(!empty($qr)) {
                $config["channel_balance"] = $qr["balance"];
            }
        }
        $order = array_merge($order, $config);
        $order["channel_company_balance"] = 100 - $order["channel_coach_balance"] - $order["channel_admin_balance"];
        $order["salesman_company_balance"] = 100 - $order["salesman_coach_balance"] - $order["salesman_admin_balance"];
        if(!empty($order["channel_id"])) {
            $channel = $this->dataInfo(["id" => $order["channel_id"], "status" => 2]);
            if(!empty($channel)) {
                $channel["channel_id"] = $channel["id"];
                $salesman_model = new Salesman();
                $salesman = $salesman_model->dataInfo(["id" => $channel["salesman_id"], "status" => 2]);
                if(!empty($salesman) && getPromotionRoleAuth(4, $order["uniacid"]) != 0) {
                    $order["salesman_balance"] = 0 <= $salesman["balance"] ? $salesman["balance"] : $order["salesman_balance"];
                    $order["salesman_id"] = $salesman["id"];
                }
            } else {
                $channel["channel_id"] = 0;
            }
        }
        if($config["salesman_channel_fx_type"] == 2 && !empty($order["salesman_id"])) {
            $order["channel_balance"] = $order["channel_balance"] < $order["salesman_balance"] ? $order["channel_balance"] : $order["salesman_balance"];
            $order["salesman_balance"] = $order["salesman_balance"] - $order["channel_balance"];
        }
        $addclockBalance_model = new AddClockBalance();
        if(!empty($channel)) {
            $order["channel_balance"] = $addclockBalance_model->getObjBalance($order, $order["channel_balance"], 4, $channel["admin_id"]);
        }
        if(!empty($salesman)) {
            $order["salesman_balance"] = $addclockBalance_model->getObjBalance($order, $order["salesman_balance"], 3, $salesman["admin_id"]);
        }
        if(empty($order["channel_balance"]) || $order["channel_balance"] <= 0) {
            $order["channel_id"] = 0;
        }
        if(empty($order["salesman_balance"]) || $order["salesman_balance"] <= 0) {
            $order["salesman_id"] = 0;
        }
        return $order;
    }
    public function checkAuthData($data)
    {
        $cap_dis[] = ["user_id", "=", $data["id"]];
        $cap_dis[] = ["status", "in", [1, 2, 3, 4]];
        $cap_info = $this->where($cap_dis)->order("id desc")->find();
        $cap_info = !empty($cap_info) ? $cap_info->toArray() : [];
        $arr["channel_status"] = !empty($cap_info) ? $cap_info["status"] : -1;
        $arr["channel_text"] = !empty($cap_info) ? $cap_info["sh_text"] : "";
        $arr["wallet_status"] = in_array($arr["channel_status"], [2, 3]) ? 1 : 0;
        return $arr;
    }
    public function getBindSalesmanChannel()
    {
        $dis = ["b.status" => 2];
        $data = $this->alias("a")->join("massage_salesman_list b", "a.salesman_id = b.id")->where($dis)->where("a.status", ">", -1)->field("a.id")->group("a.id")->column("a.id");
        return $data;
    }
    public function getUserChannel($user_id, $uniacid, $admin_id = 0, $salesman_id = 0)
    {
        $dis[] = ["user_id", "=", $user_id];
        $dis[] = ["status", ">", -1];
        $channel = $this->dataInfo($dis);
        if(!empty($channel) && $channel["status"] != 2) {
            $this->dataUpdate(["id" => $channel["id"]], ["status" => 2, "is_qr" => 1, "sh_time" => time()]);
        }
        if(empty($channel)) {
            $user_model = new User();
            $user = $user_model->dataInfo(["id" => $user_id]);
            $insert = ["uniacid" => $uniacid, "status" => 2, "user_id" => $user_id, "admin_id" => $admin_id, "salesman_id" => $salesman_id, "user_name" => $user["nickName"], "mobile" => $user["phone"], "is_qr" => 1, "create_time" => time(), "sh_time" => time()];
            $this->dataAdd($insert);
            $channel_id = $this->getLastInsID();
        } else {
            $channel_id = $channel["id"];
        }
        return $channel_id;
    }
}

